package com.etc.admin.controller;

import com.etc.dao.FlashGoodsDao;
import com.etc.dao.FlashGoodsStockDao;
import com.etc.dao.GoodsDao;
import com.etc.entity.*;
import com.etc.entity.po.FlashGoodsMessagePo;
import com.etc.service.FlashGoodsService;
import com.etc.service.OneLevelService;
import com.etc.service.TwoLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名:com.etc.admin.controller.AdminFlashGoodsController
 * 描述：管理员秒杀商品控制
 * 作者:吴仕泉
 * 时间:2019/5/29 19:15
 */
@Controller
@RequestMapping("adminflashgoods.action")
public class AdminFlashGoodsController {

    @Autowired
    FlashGoodsService  flashGoodsService;

    @Autowired
    OneLevelService oneLevelService;

    @Autowired
    TwoLevelService twoLevelService;

    @Autowired
    GoodsDao goodsDao;

    @Autowired
    FlashGoodsDao flashGoodsDao;

    @Autowired
    FlashGoodsStockDao flashGoodsStockDao;

    //管理员查询商品基本信息
    @RequestMapping("admininquireflashgoods")
    public ModelAndView adminInquireFlashGoods(Integer fsid){
        ModelAndView mv = new ModelAndView("/admin/flashgoods");
        List<FlashGoodsMessagePo> list =flashGoodsService.selectFlashSaleGoodsMessageBySaleId(fsid);
        mv.addObject("goods",list);
        return mv;
    }

    //查询一级商品分类
    @RequestMapping("findonelevel")
    @ResponseBody
    public List<OneLevel> findOnelevel(){
        List<OneLevel> list = oneLevelService.listOneLevel();
       // list.forEach(System.out::println);
        return list;
    }

    //查询一级商品分类
    @RequestMapping("findtwolevel")
    @ResponseBody
    public List<TwoLevel> findTwolevel(Integer onlevelid){
       List<TwoLevel> list = twoLevelService.listTwoLevelByOneLeveId(onlevelid);
       // list.forEach(System.out::println);
        return list;
    }

    //查询还未加入本场秒杀活动的商品
    @RequestMapping("findgoodsnotactivity")
    @ResponseBody
    public List<Goods> findGoodsNotActivity(Integer twolevelid, Integer flashorderid){
        //System.out.println(twolevelid+"::"+flashorderid);
        List<Goods> list = goodsDao.listGoodsByFlashSlarId(flashorderid,twolevelid);
        return list;
    }

    //添加商品
    @RequestMapping("addflashgoods")
    @ResponseBody
    public List<String>  addfindGoods(Integer goodsid, Integer flashorderid,Integer num,Integer price){
        FlashGoods fgs = new FlashGoods();
        fgs.setFlashgoodsprice(price.doubleValue());
        fgs.setGoodnum(num);
        fgs.setGoodsid(goodsid);
        fgs.setFlashsaleid(flashorderid);
       int i =  flashGoodsDao.insertSelective(fgs);
        FlashGoodsStock flashGoodsStock = new FlashGoodsStock();
        flashGoodsStock.setStocknum(num);
        flashGoodsStock.setFlashgoodsid(fgs.getFlashgoodsid());
        int n = flashGoodsStockDao.insertSelective(flashGoodsStock);
       List<String> list = new ArrayList<>();
       if(i+n<2){
           list.add( "添加失败");
       }
        list.add( "添加成功");
       return list;
    }

}
